package view;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import org.bushe.swing.event.EventBus;

import constants.Events;


/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class PlayerSettings extends javax.swing.JFrame implements KeyListener{
	private AbstractAction okAction;
	private AbstractAction cancelAction;
	private JButton cancelButton;
	private JButton okButton;
	private JLabel labelP4;
	private JLabel LabelP3;
	private JLabel labelP2;
	private JLabel labelP1;
	private JTextField p4Name;
	private JTextField p3Name;
	private JTextField p2Name;
	private JTextField p1Name;
	private View parent;

	//I know this should not be in view. 
	// But I can't see another way for the moment. 
	private String[] players = new String[4];

	public PlayerSettings(View parent) {
		super();
		this.parent = parent;
		initGUI();
		setLocationRelativeTo(null);
		setFocusable(true);
		addKeyListener(this);
	}

	private void initGUI() {
		JLabel[] names = parent.getPlayerNames();

		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			getContentPane().setBackground(new java.awt.Color(255,255,255));
			this.setTitle("Player settings");
			{
				p1Name = new JTextField();
				getContentPane().add(p1Name);
				p1Name.setText(names[0].getText());
				p1Name.setBounds(91, 24, 161, 22);
			}
			{
				p2Name = new JTextField();
				getContentPane().add(p2Name);
				p2Name.setText(names[1].getText());
				p2Name.setBounds(91, 63, 161, 22);
			}
			{
				p3Name = new JTextField();
				getContentPane().add(p3Name);
				p3Name.setText(names[2].getText());
				p3Name.setBounds(91, 105, 161, 22);
			}
			{
				p4Name = new JTextField();
				getContentPane().add(p4Name);
				p4Name.setText(names[3].getText());
				p4Name.setBounds(91, 146, 161, 22);
			}
			{
				okButton = new JButton();
				getContentPane().add(okButton);
				okButton.setText("OK");
				okButton.setBounds(300, 271, 59, 22);
				okButton.setAction(getOkAction());
			}
			{
				cancelButton = new JButton();
				getContentPane().add(cancelButton);
				getContentPane().add(getLabelP1());
				getContentPane().add(getLabelP2());
				getContentPane().add(getLabelP3());
				getContentPane().add(getLabelP4());
				cancelButton.setText("Cancel");
				cancelButton.setBounds(214, 271, 75, 22);
				cancelButton.setAction(getCancelAction());
			}
			pack();
			this.setSize(400, 340);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}	

	private AbstractAction getCancelAction() {
		if(cancelAction == null) {
			cancelAction = new AbstractAction("Cancel", null) {
				public void actionPerformed(ActionEvent evt) {

					dispose();
				}
			};
		}
		return cancelAction;
	}

	private AbstractAction getOkAction() {
		if(okAction == null) {
			okAction = new AbstractAction("OK", null) {
				public void actionPerformed(ActionEvent evt) {
					for(int i=0; i < 4; i++){
						if(p1Name.getText() != null){
							players[0] = p1Name.getText();
						}
						if(p2Name.getText() != null){
						}
						if(p1Name.getText() != null){
							players[1] = p2Name.getText();
						}
						if(p1Name.getText() != null){
							players[2] = p3Name.getText();
						}
						if(p1Name.getText() != null){
							players[3] = p4Name.getText();
						}
					}
					EventBus.publish(Events.PLAYERNAME.toString(),players);
					dispose();
				}
			};
		}
		return okAction;
	}

	private JLabel getLabelP1() {
		if(labelP1 == null) {
			labelP1 = new JLabel();
			labelP1.setText("Player 1");
			labelP1.setBounds(12, 27, 55, 16);
		}
		return labelP1;
	}

	private JLabel getLabelP2() {
		if(labelP2 == null) {
			labelP2 = new JLabel();
			labelP2.setText("Player 2");
			labelP2.setBounds(12, 66, 72, 16);
		}
		return labelP2;
	}

	private JLabel getLabelP3() {
		if(LabelP3 == null) {
			LabelP3 = new JLabel();
			LabelP3.setText("Player 3");
			LabelP3.setBounds(12, 108, 61, 16);
		}
		return LabelP3;
	}

	private JLabel getLabelP4() {
		if(labelP4 == null) {
			labelP4 = new JLabel();
			labelP4.setText("Player 4");
			labelP4.setBounds(12, 149, 67, 16);
		}
		return labelP4;
	}

	@Override
	public void keyPressed(KeyEvent e) {
		Character c = e.getKeyChar();
		if(c.equals('\n')) {
			okButton.doClick();
		}
		System.out.println(e.getKeyCode());
		if(e.getKeyCode() == 27)
			cancelButton.doClick();		
	}

	@Override
	public void keyReleased(KeyEvent e) {}

	@Override
	public void keyTyped(KeyEvent e) {}
}